package gui.opcoes;

import javax.microedition.lcdui.*;
import recados.Recados;

public class Settings extends Form implements CommandListener {

    private final Command cmdSalvar;
    private final Command cmdVoltar;
    private final Recados midlet;
    private Displayable callBack;
    private final ChoiceGroup miscelanea;
    private final ChoiceGroup mensagens;
    private final ChoiceGroup atualizarUsuarios;
    private final ChoiceGroup atualizaChat;
    private final ChoiceGroup chat;
    private final ChoiceGroup atualizaMensagens;

    public Settings(Recados midlet) {
        super("Opções");

        this.midlet = midlet;

        mensagens = new ChoiceGroup("Mensagens:", Choice.MULTIPLE);
        mensagens.append("Exibir smiles", null);
        mensagens.append("Carregar imagens", null);
        append(mensagens);

        String[] atualizacoes = new String[]{"Manual", "1 min", "2 min", "3 min", "4 min", "5 min"};

        atualizaMensagens = new ChoiceGroup("Atualizar:", Choice.POPUP, atualizacoes, null);
        append(atualizaMensagens);

        append(new Spacer(0, 10));
        append(new StringItem("Usuarios:", null));

        atualizarUsuarios = new ChoiceGroup("Atualizar Lista:", Choice.POPUP, atualizacoes, null);
        append(atualizarUsuarios);

        append(new Spacer(0, 10));

        chat = new ChoiceGroup("Chat:", Choice.MULTIPLE, new String[]{"Exibir smiles", "Carregar imagens"}, null);
        append(chat);

        atualizaChat = new ChoiceGroup("Atualizar:", Choice.POPUP, atualizacoes, null);
        append(atualizaChat);

        append(new Spacer(0, 10));

        miscelanea = new ChoiceGroup("Outros", Choice.MULTIPLE);
        miscelanea.append("Flush", null);
        miscelanea.append("Usar HTTP", null);
        miscelanea.append("Exibir progresso de transferencia de arquivos em bytes", null);
        miscelanea.append("Alertas de erros de tarefas em segundo plano", null);

        append(miscelanea);

        cmdSalvar = new Command("Salvar", Command.OK, 0);
        cmdVoltar = new Command("Voltar", Command.BACK, 1);

        addCommand(cmdSalvar);
        addCommand(cmdVoltar);

        setCommandListener(this);

        atualizaMensagens.setSelectedIndex(midlet.getConfig().getAtualizaMensagens(), true);
        atualizarUsuarios.setSelectedIndex(midlet.getConfig().getAtualizaUsuarios(), true);
        atualizaChat.setSelectedIndex(midlet.getConfig().getAtualizaChat(), true);

        boolean[] msgflags=new boolean[]{midlet.getConfig().isSmilesMensagens(),midlet.getConfig().isImagensMensagens()};
        mensagens.setSelectedFlags(msgflags);
        
        boolean[] chatFlags = new boolean[]{midlet.getConfig().isSmilesChat(),midlet.getConfig().isImagensChat()};
        chat.setSelectedFlags(chatFlags);
        
        boolean[] flags = new boolean[]{midlet.getConfig().isFlush(), midlet.getConfig().isHttp(), midlet.getConfig().isBytes(), midlet.getConfig().isErros()};
        miscelanea.setSelectedFlags(flags);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSalvar) {
            midlet.getConfig().setAtualizaMensagens(atualizaMensagens.getSelectedIndex());
            midlet.getConfig().setAtualizaUsuarios(atualizarUsuarios.getSelectedIndex());
            midlet.getConfig().setAtualizaChat(atualizaChat.getSelectedIndex());
            midlet.getConfig().setSmilesMensagens(mensagens.isSelected(0));
            midlet.getConfig().setImagensMensagens(mensagens.isSelected(1));
            midlet.getConfig().setSmilesChat(chat.isSelected(0));
            midlet.getConfig().setImagensChat(chat.isSelected(1));
            midlet.getConfig().setFlush(miscelanea.isSelected(0));
            midlet.getConfig().setHttp(miscelanea.isSelected(1));
            midlet.getConfig().setBytes(miscelanea.isSelected(2));
            midlet.getConfig().setErros(miscelanea.isSelected(3));

            midlet.getConfig().salva();

            if (midlet.isLogado()) {
                midlet.startAtualizador();
            }

            Recados.setCurrent(callBack);
        } else if (c == cmdVoltar) {
            Recados.setCurrent(callBack);
        }
    }

    public void setBack(Displayable back) {
        this.callBack = back;
    }
}
